package com.tabview.difficultpoint;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.xudow.app.ui.BaseFragment;


public abstract class BaseUiFragment extends BaseFragment {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
        initListener();
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
    }

    protected View findViewById(int id) {
        return getView().findViewById(id);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    /**
     * 结束fragment
     */
    public void popBackStack() {

        if (getActivity() == null || getActivity().getSupportFragmentManager().getBackStackEntryCount() == 1) {
            return;
        }
        getActivity().getSupportFragmentManager().popBackStack();
    }

    /**
     * 关闭软件键盘
     */
    protected void hideSoftInput() {
        View view = getActivity().getCurrentFocus();
        InputMethodManager mInputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (view != null) {
            mInputMethodManager.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
        }

    }

    protected void clearMemory() {
    }

    protected abstract void initData();

    protected abstract void initListener();

    protected boolean isVisible;

    /**
     * 在这里实现Fragment数据的缓加载.
     *
     * @param isVisibleToUser
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }

    protected void onVisible() {
        lazyLoad();
    }

    protected abstract void lazyLoad();

    protected void onInvisible() {
    }

}
